<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <style>
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>系统设置<small>角色管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统设置</el-breadcrumb-item>
                    <el-breadcrumb-item>角色管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="编码/名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
                        <el-table-column prop="keyword" label="角色编码" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="编码">
                                                        <el-input v-model="formData.keyword"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="名称">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="权限信息" name="second">
									<div class="checkScro1">
                                        <table class="datatable">
                                            <thead>
                                            <tr>
                                                <th>选择</th>
                                                <th>权限名称</th>
                                                <th>权限编码</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr v-for="p in tableData">
                                                <td>
                                                    <input :id="p.id" v-model="permissionIds" type="checkbox" :value="p.id">
                                                </td>
                                                <td><label :for="p.id">{{p.name}}</label></td>
                                                <td><label :for="p.id">{{p.keyword}}</label></td>
                                            </tr>
                                            </tbody>
                                        </table>
									</div>
                                    </el-tab-pane>
                                    <el-tab-pane label="菜单信息" name="three">
                                        <!-- default-expanded-keys和default-checked-keys设置默认展开和默认选中的节点。
                                        需要注意的是，此时必须设置node-key，其值为节点数据中的一个字段名，
                                        该字段在整棵树中是唯一的。-->
                                        <el-tree
                                                :data="tableData2"
                                                show-checkbox
                                                node-key="id"
                                                ref="tree"
                                                :default-expanded-keys="[]"
                                                :default-checked-keys="[]"
                                                :props="defaultProps">
                                        </el-tree>
                                        <!--获取和设置各有两种方式：通过 node 或通过 key。
                                        如果需要通过 key 来获取或设置，则必须设置node-key 和 ref="tree"
                                        获取:console.log(this.$refs.tree.getCheckedKeys());
                                        设置:this.$refs.tree.setCheckedKeys([3]);   -->
                                        <!--<div class="buttons">
                                            <el-button @click="getCheckedKeys">通过 key 获取</el-button>
                                        </div>-->
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="编码">
                                                        <el-input v-model="formData.keyword"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="名称">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="权限信息" name="second">
                                        <div class="checkScro1">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>权限名称</th>
                                                    <th>权限编码</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="p in tableData">
                                                    <td>
                                                        <input :id="p.id" v-model="permissionIds" type="checkbox" :value="p.id">
                                                    </td>
                                                    <td><label :for="p.id">{{p.name}}</label></td>
                                                    <td><label :for="p.id">{{p.keyword}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                    <el-tab-pane label="菜单信息" name="three">
                                        <!-- default-expanded-keys和default-checked-keys设置默认展开和默认选中的节点。
                                        需要注意的是，此时必须设置node-key，其值为节点数据中的一个字段名，
                                        该字段在整棵树中是唯一的。-->
                                        <el-tree
                                                :data="tableData2"
                                                show-checkbox
                                                node-key="id"
                                                ref="tree"
                                                :default-expanded-keys="menuIds"
                                                :default-checked-keys="menuIds"
                                                :props="defaultProps">
                                        </el-tree>
                                        <!--获取和设置各有两种方式：通过 node 或通过 key。
                                        如果需要通过 key 来获取或设置，则必须设置node-key 和 ref="tree"
                                        获取:console.log(this.$refs.tree.getCheckedKeys());
                                        设置:this.$refs.tree.setCheckedKeys([3]);   -->
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                </div>
            </div>
        </div>
    </body>

    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                activeName:'first',//添加/编辑窗口Tab标签名称
                pagination: {//分页相关属性
                    currentPage: 1,
                    pageSize:10,
                    total:100,
                    queryString:null,
                },
                dataList: [],//列表数据
                formData: {},//表单数据
                tableData:[],//新增和编辑表单中对应的检查项列表数据
                permissionIds:[],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
                tableData2:[],//新增和编辑表单中对应的caidan列表数据 data2
                menuIds:[],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit:false//控制编辑窗口显示/隐藏
                ,
                //菜单数据 list<map<string,obj>>
                /*data2: [{
                    id: 1,
                    label: '一级 1',
                    children: [{
                        id: 4,
                        label: '二级 1-1',
                        children: [{
                            id: 9,
                            label: '三级 1-1-1'
                        }, {
                            id: 10,
                            label: '三级 1-1-2'
                        }]
                    }]
                }, {
                    id: 2,
                    label: '一级 2',
                    children: [{
                        id: 5,
                        label: '二级 2-1'
                    }, {
                        id: 6,
                        label: '二级 2-2'
                    }]
                }, {
                    id: 3,
                    label: '一级 3',
                    children: [{
                        id: 7,
                        label: '二级 3-1'
                    }, {
                        id: 8,
                        label: '二级 3-2'
                    }]
                }],*/
                defaultProps: {
                    //指定子树为节点对象的某个属性值 string
                    children: 'children',
                    //指定节点标签为节点对象的某个属性值 string, function(data, node)
                    label: 'name'
                }
            },
            created() {
                this.findPage();
            },
            methods: {
                //编辑
                handleEdit() {
                    //发送ajax请求将模型数据提交到后台处理
                    axios.post("../Role/update.do?permissionIds="+this.permissionIds
                                + "&menuIds="+this.$refs.tree.getCheckedKeys(),
                                this.formData)
                        .then(resp =>{
                            if(resp.data.flag){
                                //隐藏编辑窗口
                                this.dialogFormVisible4Edit = false;
                                this.$message({
                                    message: resp.data.message,
                                    type: 'success'
                                });
                                this.findPage();
                            }else{
                                this.$message.error(resp.data.message);
                            }
                        });
                },
                //添加
                handleAdd () {
                    //发送ajax请求将模型数据提交到后台处理
                    axios.post(
                        "../Role/add.do?permissionIds=" + this.permissionIds
                        + "&menuIds="+this.$refs.tree.getCheckedKeys(),
                        this.formData
                    ).then((resp)=> {
                        //关闭新增窗口
                        this.dialogFormVisible = false;
                        if(resp.data.flag){
                            //新增成功，弹出成功提示
                            this.$message({
                                message: resp.data.message,
                                type: 'success'
                            });
                            //刷新数据
                            this.findPage();
                        }else{
                            //新增失败，弹出错误提示
                            this.$message.error(resp.data.message);
                        }
                    });
                },
                //分页查询
                findPage() {
                    let param = {
                        currentPage:this.pagination.currentPage,//页码
                        pageSize:this.pagination.pageSize,//每页显示的记录数
                        queryString:this.pagination.queryString//查询条件
                    }
                    axios.post("../Role/findPage.do",param)
                        .then(resp =>{
                            if(resp.data.flag){
                               this.pagination.total = resp.data.data.total;
                               this.dataList = resp.data.data.rows;
                            }else{
                                this.$message.error(resp.data.message);
                            }
                        });

                },
                // 重置表单
                resetForm() {
                    this.formData={};
                },
                // 弹出添加窗口
                handleCreate() {
                    this.resetForm();
                    this.dialogFormVisible = true;
                    //默认切换到第一个标签页
                    this.activeName= "first";
                    //重置数据
                    this.permissionIds = [];
                    this.menuIds = [];
                    //发送请求获取初始数据
                    axios.get("../Role/findAll.do")
                        .then(resp => {
                            if(resp.data.flag){
                                this.tableData = resp.data.data.permissions;
                                this.tableData2 = resp.data.data.menus;
                            } else{
                                this.$message.error(resp.data.message);
                            }
                        });
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    this.dialogFormVisible4Edit = true;
                    //默认选中第一个标签页
                    this.activeName='first';
                    //重置数据
                    this.permissionIds = [];
                    this.menuIds = [];
                    //回显role数据
                    axios.get("../Role/findById.do?id="+row.id).then(res=>{
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? "success":"error"
                        });
                        if(res.data.flag){
                            this.formData = res.data.data;
                            //回显权限和菜单(勾选)
                            axios.get("../Role/findAll.do").then(res=>{
                                this.$message({
                                    message: res.data.message,
                                    type: res.data.flag ? "success":"error"
                                });
                                if(res.data.flag){
                                    //初始数据
                                    this.tableData = res.data.data.permissions;
                                    this.tableData2 = res.data.data.menus;
                                    //获取勾选数据
                                    axios.get("../Role/findPermissionAndMenuByRoleId.do?id="+row.id).then(res=>{
                                        this.permissionIds = res.data.data.pids;
                                        this.menuIds = res.data.data.mids;
                                    })
                                }
                            })
                        }
                    });

                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                },
                // 删除
                handleDelete(row) {
                    console.log("删除 row ",row);
                    this.$confirm("确认删除当前选中记录吗？","提示",{type:'warning'})
                        .then(()=>{
                            axios.get("../Role/delete.do?id="+row.id)
                                .then(resp =>{
                                    if(resp.data.flag){
                                        this.$message({
                                            message:resp.data.message,
                                            type:"success"
                                        });
                                        this.findPage();
                                    }else{
                                        this.$message.error(resp.data.message);
                                    }
                                });
                        });

                }
            }
        })
    </script>
</html>
